#include "picanimationwid.h"
#include <QTimer>
#include "protreeitem.h"
#include <QPainter>
#include <QDebug>

PicAnimationWid::PicAnimationWid(QWidget *parent)
    :QWidget(parent)
    ,_factor(0.0)
    ,_cur_item(nullptr)
    ,_bstart(false)
{
    _timer = new QTimer(this);
    connect(_timer,&QTimer::timeout,this,&PicAnimationWid::TimeOut);
}

PicAnimationWid::~PicAnimationWid()
{
    _timer->stop();
}

void PicAnimationWid::SetPixmap(QTreeWidgetItem *item)
{
    qDebug() << "void PicAnimationWid::SetPixmap(QTreeWidgetItem *item)";
    if(!item){
        qDebug()<<"item is null";
        return;
    }
    auto* tree_item = dynamic_cast<ProTreeItem*>(item);
    auto path = tree_item->GetPath();
    qDebug()<<path;
    _pixmap1.load(path);
    if(_pixmap1.isNull()){
        qDebug()<<"SetPixmap _pixmap1 is null";
    }
    _cur_item = tree_item;
    if(_map_items.find(path) == _map_items.end()){
        _map_items[path] = tree_item;
        qDebug() << "SetPixmap path is " << path << endl;
        //发送更新预览列表逻辑
        emit SigPreList(item);
    }
    //实现选中的效果
    emit SigSelectItem(item);

    auto* next_item = tree_item->GetNextItem();
    if(!next_item){
        return;
    }
    auto next_path = next_item->GetPath();
    _pixmap2.load(next_path);
    if(_map_items.find(next_path) == _map_items.end()){
        _map_items[next_path] = next_item;
        //发送更新预览列表逻辑
        emit SigPreList(next_item);
    }
}

void PicAnimationWid::Start()
{
    emit SigStart();
    emit SigStartMusic();
    _factor = 0;
    _timer->start(25);
    _bstart = true;
}

void PicAnimationWid::stop()
{
    emit SigStop();
    emit SigStopMusic();
    _timer->stop();
    _factor=0;
    _bstart=false;
}

void PicAnimationWid::SlideNext()
{
    stop();
    if(!_cur_item){
        return;
    }
    auto* cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
    auto* next_item = cur_pro_item->GetNextItem();
    if(!next_item){
        return;
    }
    SetPixmap(next_item);
    update();
}

void PicAnimationWid::SlidePre()
{
    stop();
    if(!_cur_item){
        return;
    }
    auto* cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
    auto* pre_item = cur_pro_item->GetPreItem();
    if(!pre_item){
        return;
    }
    SetPixmap(pre_item);
    update();
}

void PicAnimationWid::SlotUpSelectShow(QString path)
{
    auto iter = _map_items.find(path);
    if(iter == _map_items.end()){
        return;
    }
    UpSelectPixmap(iter.value());
    update();
}

void PicAnimationWid::SlotStartOrStop()//实现控制播放的逻辑
{
    if(!_bstart){
        _factor = 0;
        _timer->start(25);
        _bstart = true;
        //发送使开启音乐
        emit SigStartMusic();
    }else{
        _timer->stop();
        _factor = 0;
        update();
        _bstart = false;
        //发送信号使关闭音乐
        emit SigStopMusic();
    }
}

void PicAnimationWid::paintEvent(QPaintEvent *event)//负责轮播图中图片若隐若现的逻辑
{
    if(_pixmap1.isNull()){
        qDebug()<<"_pixmap1.isNull";
        return;
    }
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    QRect rect = geometry();
    int w = rect.width();
    int h = rect.height();
    _pixmap1 = _pixmap1.scaled(w,h,Qt::KeepAspectRatio);
    int alpha = 255*(1.0f-_factor);
    QPixmap alphaPixmap(_pixmap1.size());
    alphaPixmap.fill(Qt::transparent);

    QPainter p1(&alphaPixmap);
    p1.setCompositionMode(QPainter::CompositionMode_Source);
    p1.drawPixmap(0,0,_pixmap1);
    p1.setCompositionMode(QPainter::CompositionMode_Destination);
    p1.fillRect(alphaPixmap.rect(),QColor(0,0,0,alpha));
    p1.end();
    int x = (w - _pixmap1.width()) / 2;
    int y = (h - _pixmap1.height()) / 2;
    painter.drawPixmap(x, y, alphaPixmap);

    if(_pixmap2.isNull()){
        return;
    }
    _pixmap2 = _pixmap2.scaled(w,h,Qt::KeepAspectRatio);
    alpha = 255*(_factor);
    QPixmap alphaPixmap2(_pixmap2.size());
    alphaPixmap2.fill(Qt::transparent);
    QPainter p2(&alphaPixmap2);
    p2.setCompositionMode(QPainter::CompositionMode_Source);
    p2.drawPixmap(0,0,_pixmap2);
    p2.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p2.fillRect(alphaPixmap2.rect(),QColor(0,0,0,alpha));
    p2.end();
    x = (w - _pixmap2.width()) / 2;
    y = (h - _pixmap2.height()) / 2;
    painter.drawPixmap(x, y, alphaPixmap2);
    //qDebug()<<"成功执行paintEvent逻辑";
}

void PicAnimationWid::UpSelectPixmap(QTreeWidgetItem *item)
{
    if(!item){
        return;
    }
    auto* tree_item = dynamic_cast<ProTreeItem*>(item);
    auto path = tree_item->GetPath();
    _pixmap1.load(path);
    _cur_item = tree_item;
    if(_map_items.find(path) == _map_items.end()){
        _map_items[path]=tree_item;
    }
    auto* next_item = tree_item->GetNextItem();
    if(!next_item){
        return;
    }
    auto next_path = tree_item->GetPath();
    _pixmap2.load(next_path);
    if(_map_items.find(next_path) == _map_items.end()){
        _map_items[next_path] = next_item;
    }
}

void PicAnimationWid::TimeOut()
{
    if(!_cur_item){
        stop();
        update();
        return;
    }
    _factor += 0.01;
    if(_factor >= 1){//当前图片展示完毕开始展示下一张图片
        _factor = 0;
        auto* cur_pro_item = dynamic_cast<ProTreeItem*>(_cur_item);
        auto* next_pro_item = cur_pro_item->GetNextItem();
        if(!next_pro_item){//如果没有下一张图片了
            stop();
            update();
            return;
        }
        SetPixmap(next_pro_item);
        update();
        return;
    }
    update();
}


